﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：TypeProduct.ascx.cs
//
//
//文件功能描述：根据不同显示前台页面的商品.
//
//
//创建标识：2007.11.16 王勇
//创建描述：对商品展示的部分描述..
 //
 //修改标识：2007.11.29
//修改描述：实现了分页(利用存储过程)
//------------------------------------*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Common_Controls_TypeProduct : System.Web.UI.UserControl
{
    //private static PagedDataSource pds;  //分页数据源

    localhost.ProductService myOperate = new localhost.ProductService();  //操作类

    private static int iPageCount;  //总的页数

    private static int iRecords;    //总的记录数

    private string typeProduct;  //绑定商品类型

    /// <summary>
    /// 商品类型的属性
    /// </summary>
    public string TypeProduct
    {
       set { typeProduct = value; } 
       get
       {

           //临时变量
           string strTemp = string.Empty;

           switch (typeProduct)
           {
               case "热卖商品":
                   strTemp = "HotProduct";
                   break;
               case "推荐商品":
                   strTemp = "CommandProduct";
                   break;
               case "最新商品":
                   strTemp = "NewProduct";
                   break;
               default:
                   strTemp = typeProduct;
                   break;
           }
           return strTemp;
       }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           //如果是第一次加载
            BindDataList(1);

            //获取页的总数
            GetProductCount();

            //绑定页的选择索引
            BindDropdownListPageIndex();
                   
        }
        
    }

   
    /// <summary>
    /// 绑定商品列表显示
    /// </summary>
    /// <param name="iPageIndex">当前页的数</param>
    protected void BindDataList(int iPageIndex)
    {
        //页面大小
        int iPageSize = 8;
        
        //异步调用WebService
        if (TypeProduct == "全部商品")
        {
           //如果是全部商品
            myOperate.GetAllProductListCompleted += new localhost.GetAllProductListCompletedEventHandler(myOperate_GetAllProductListCompleted);

            myOperate.GetAllProductListAsync(iPageSize,iPageIndex,DropDownList1.SelectedValue);

        }
        else
        {
           //否则
            myOperate.GetProductListByProductTypeCompleted += new localhost.GetProductListByProductTypeCompletedEventHandler(myOperate_GetProductListByProductTypeCompleted);

            myOperate.GetProductListByProductTypeAsync(iPageSize, iPageIndex, TypeProduct,DropDownList1.SelectedValue);

        }

        //显示当前页
        lblPage.Text = iPageIndex.ToString();

        if (iPageIndex == 1)
        {
            //如果当前页为第一页
            ddlPage.SelectedValue = "0"; 
        }
        else
        {
            ddlPage.SelectedValue = (iPageIndex - 1).ToString();
        }


        //显示分页按钮的可用性
        ShowButtonPaging();
        
    }



    /// <summary>
    /// 回调函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void myOperate_GetProductListByProductTypeCompleted(object sender, localhost.GetProductListByProductTypeCompletedEventArgs e)
    {
           //DataSet保存结果集
            DataSet dsProductList = new DataSet();

            if (e.Result!=null)
            {
                //如果返回结果不空
                dsProductList = e.Result;

                //DataView视图作数据源
                DataView dvProductList = dsProductList.Tables[0].DefaultView;

                //绑定数据源
                dlstTypeProduct.DataSource = dvProductList;
                dlstTypeProduct.DataBind();
            }
    }


     ///<summary>
     ///回调函数
     ///</summary>
     ///<param name="sender"></param>
     ///<param name="e"></param>
    void myOperate_GetAllProductListCompleted(object sender, localhost.GetAllProductListCompletedEventArgs e)
    {
           //DataSet保存结果集
            DataSet dsProductList = new DataSet();

            if (e.Result!=null)
            {
                //如果返回结果不空
                dsProductList = e.Result;

                //DataView视图作数据源
                DataView dvProductList = dsProductList.Tables[0].DefaultView;

                //绑定数据源
                dlstTypeProduct.DataSource = dvProductList;
                dlstTypeProduct.DataBind();
            }
    }


    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {
        if (typeProduct == "全部商品")
        {
            //如果显示的是全部商品
            iRecords = myOperate.GetAllProductCount();
        }
        else
        {
            //否则
            iRecords = myOperate.GetProductCoutByProductTyppe(TypeProduct);
        }

        if (iRecords % 8 == 0)
        {
            //记录数刚好可以整除页大小
            iPageCount = iRecords / 8;
        }
        else
        {
            //记录数不能整除页大小
            iPageCount = Convert.ToInt32(iRecords / 8) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }


    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {
        ListItem lt;
        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            ddlPage.Items.Add(lt);
        }
    }


    
    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {

        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;
        
        if (lblPage.Text == "1")
        {
            //如果是第一页，则将首页与上一页的按钮设为不可用
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
        }
        else if (int.Parse(lblPage.Text) == iPageCount)
        {
            //如果是最后一页，则将末页与下一页设为不可用
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        else
        {
            //其他情况全部为可用
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;
        
        }
    }


   /// <summary>
   /// 选择排序方式
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 选择页的索引
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        BindDataList(1);
    }


    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnPre_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
        BindDataList(iPageCount);
    }


    /// <summary>
    /// 数据项绑定时激发的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dlstTypeProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;

        ((ImageButton)e.Item.FindControl("imageBtnProducter")).Width = Unit.Parse("110px");
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).Height = Unit.Parse("110px");

        ((ImageButton)e.Item.FindControl("imageBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID; 

        ((ImageButton)e.Item.FindControl("imageBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;
    }
}
